package blow.sky.quoted.controller;

import blow.sky.quoted.entity.ClientViewRequest;
import blow.sky.quoted.entity.QuoteRequest;
import blow.sky.quoted.entity.UserViewRequest;
import blow.sky.quoted.response.MsgResponse;
import blow.sky.quoted.service.QuoteRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName QuoteRequestController
 * @Description TODO
 * @Author 江志武
 * @Date 2021/5/25 11:07
 */

@RestController
@RequestMapping("/api/request")
public class QuoteRequestController {
    @Autowired
    QuoteRequestService quoteRequestService;

    @PostMapping("/updateOne")
    public MsgResponse getOne(@RequestBody UserViewRequest quoteDemand)
    {
        boolean flag = quoteRequestService.insert(quoteDemand);
        if (flag)
            return new MsgResponse("success");
        else
            return new MsgResponse("fail", "Format Error");
    }

    //获取所有未成功报价信息
    @GetMapping("/GetAll")
    public List<UserViewRequest> getAll()
    {
        return quoteRequestService.findALL();
    }

    //获取所有未成功报价信息
    @GetMapping("/GetAll/info")
    public List<ClientViewRequest> getAllAndInfo()
    {
        return quoteRequestService.findRequestAndInfo(quoteRequestService.findALL());
    }

    //获取发布所有未成功订单信息
    @GetMapping("/false/info")
    public List<ClientViewRequest> getAllWhereFalseAndInfo()
    {
        return quoteRequestService.findRequestAndInfo(quoteRequestService.findALLbySuccession(false));
    }

    //获取发布所有成功订单信息
    @GetMapping("/true/info")
    public List<ClientViewRequest> getAllWhereTrueAndInfo()
    {
        return quoteRequestService.findRequestAndInfo(quoteRequestService.findALLbySuccession(true));
    }


    //获取发布者所有未成功订单信息
    @GetMapping("/false/{UserId}")
    public List<UserViewRequest> getPersonWhereFalse(@PathVariable("UserId") String user)
    {
        return quoteRequestService.findALLbyRequestIdAndFalse(user);
    }
    //获取发布者所有未成功订单信息
    @GetMapping("/false/info/{UserId}")
    public List<ClientViewRequest> getPersonWhereFalseAndInfo(@PathVariable("UserId") String user)
    {
        return quoteRequestService.findRequestAndInfo(quoteRequestService.findALLbyRequestIdAndFalse(user));
    }

    //获取发布者所有成功订单信息
    @GetMapping("/true/{UserId}")
    public List<UserViewRequest> getPersonWhereTrue(@PathVariable("UserId") String user)
    {
        return quoteRequestService.findALLbyRequestIdAndTrue(user);
    }

    //获取发布者所有成功订单信息
    @GetMapping("/true/info/{UserId}")
    public List<ClientViewRequest> getPersonWhereTrueAndInfo(@PathVariable("UserId") String user)
    {
        return quoteRequestService.findRequestAndInfo(quoteRequestService.findALLbyRequestIdAndTrue(user));
    }

    //改变订单状态
    @PutMapping("/{id}/{request}")
    public MsgResponse updateRequest(@PathVariable("id") long id, @PathVariable("request") boolean request)
    {
        boolean flag =  quoteRequestService.updateIsSucceed(id,request);
        if (flag)
            return new MsgResponse("success");
        else
            return new MsgResponse("fail", "update Error,id is non-existent");
    }
}
